package lesson10.websocket;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import lesson8.chat.client.ClientInitializer;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * <br/>
 * Created on : 2018-01-30 21:15
 * @author lizebin
 */
public class Client {

	public static void main(String[] args) throws Exception {
		EventLoopGroup eventLoopGroup = new NioEventLoopGroup(1);

		try {
			Bootstrap bootstrap = new Bootstrap();
			//这里加的是 handler 而非 childHandler
			bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class).handler(new ClientInitializer());
			Channel channel = bootstrap.connect("localhost", 8899).sync().channel();
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			while(true) {
				channel.writeAndFlush(br.readLine() + "\r\n");
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			eventLoopGroup.shutdownGracefully();
		}
	}

}
